package com.book.category.service.impl;

import java.util.List;
import com.book.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.book.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.book.category.domain.Books;
import com.book.category.mapper.BookCategoryMapper;
import com.book.category.domain.BookCategory;
import com.book.category.service.IBookCategoryService;

/**
 * 图书分类Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-29
 */
@Service
public class BookCategoryServiceImpl implements IBookCategoryService 
{
    @Autowired
    private BookCategoryMapper bookCategoryMapper;

    /**
     * 查询图书分类
     * 
     * @param id 图书分类主键
     * @return 图书分类
     */
    @Override
    public BookCategory selectBookCategoryById(Long id)
    {
        return bookCategoryMapper.selectBookCategoryById(id);
    }

    /**
     * 查询图书分类列表
     * 
     * @param bookCategory 图书分类
     * @return 图书分类
     */
    @Override
    public List<BookCategory> selectBookCategoryList(BookCategory bookCategory)
    {
        return bookCategoryMapper.selectBookCategoryList(bookCategory);
    }

    /**
     * 新增图书分类
     * 
     * @param bookCategory 图书分类
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBookCategory(BookCategory bookCategory)
    {
        bookCategory.setCreateTime(DateUtils.getNowDate());
        int rows = bookCategoryMapper.insertBookCategory(bookCategory);
        insertBook(bookCategory);
        return rows;
    }

    /**
     * 修改图书分类
     * 
     * @param bookCategory 图书分类
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBookCategory(BookCategory bookCategory)
    {
        bookCategory.setUpdateTime(DateUtils.getNowDate());
        bookCategoryMapper.deleteBookByCategoryId(bookCategory.getId());
        insertBook(bookCategory);
        return bookCategoryMapper.updateBookCategory(bookCategory);
    }

    /**
     * 批量删除图书分类
     * 
     * @param ids 需要删除的图书分类主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBookCategoryByIds(Long[] ids)
    {
        bookCategoryMapper.deleteBookByCategoryIds(ids);
        return bookCategoryMapper.deleteBookCategoryByIds(ids);
    }

    /**
     * 删除图书分类信息
     * 
     * @param id 图书分类主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBookCategoryById(Long id)
    {
        bookCategoryMapper.deleteBookByCategoryId(id);
        return bookCategoryMapper.deleteBookCategoryById(id);
    }

    /**
     * 新增图书信息
     * 
     * @param bookCategory 图书分类对象
     */
    public void insertBook(BookCategory bookCategory)
    {
        List<Books> bookList = bookCategory.getBookList();
        Long id = bookCategory.getId();
        if (StringUtils.isNotNull(bookList))
        {
            List<Books> list = new ArrayList<Books>();
            for (Books book : bookList)
            {
                book.setCategoryId(id);
                list.add(book);
            }
            if (list.size() > 0)
            {
                bookCategoryMapper.batchBook(list);
            }
        }
    }
}
